package gen.tools.delivery;

import gen.tools.Widgets;
import gen.tools.structures.S2;
import gen.tools.structures.S4;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class GetParameter4SDEL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GetParameter4SDEL() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		S4 info = new S4();
		
		boolean isFTP = Boolean.parseBoolean( request.getParameter("isFTP") );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "SELECT paraName,paraVal FROM configparameter WHERE paraName LIKE 'svn%'";
		
		try {
			conn = wid.getConn();
			if ( isFTP ) {
				sql = "SELECT paraName,paraVal FROM configparameter WHERE paraName LIKE 'ftp%'";				
			}
			ps = conn.prepareStatement( sql );
			rs = ps.executeQuery();
			
			while( rs.next() ) {
				String key = rs.getString("paraName");
				String val = rs.getString("paraVal");
				if( key.contains("IP") ) {
					info.setS1( val );
				} else if( key.contains("Id") ) {
					info.setS2( val );
				} else if( key.contains("Pwd") ) {
					info.setS3( val );
				} else if( key.contains("Path") ) {
					info.setS4( val );
				}
			}
		} catch (Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) { rs.close(); }
				if ( ps !=null ) { ps.close(); }
				if ( conn !=null ) { conn.close(); }	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		response.getWriter().write( new Gson().toJson( info ) );	
	}
}
